package com.lxy.user.controller;

import com.aliyun.avatar20220130.models.QueryRunningInstanceResponse;
import com.lxy.common.domain.common.R;
import com.lxy.user.service.DigitalHumanService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Map;

@RestController
@RequestMapping("/api/digital-human")
public class AvatarController {
    
    private final DigitalHumanService digitalHumanService;
    @Autowired
    private  RedisTemplate redisTemplate;
    
    public AvatarController(DigitalHumanService digitalHumanService) {
        this.digitalHumanService = digitalHumanService;
    }
    
    /**
     * 启动RTC数字人
     * @param request 用户信息
     * @return 会话ID
     */
    @PostMapping("/start/rtc")
    public Map<String, String> startRtcInstance(@RequestBody StartRequest request) {
        String sessionId = digitalHumanService.startRtcInstance(
            request.getUserId(), 
            request.getUserName()
        );
        return Collections.singletonMap("sessionId", sessionId);
    }
    
//    /**
//     * 启动RTMP推流数字人
//     * @param request 启动请求
//     * @return 会话ID
//     */
//    @PostMapping("/start/rtmp")
//    public Map<String, String> startRtmpInstance(@RequestBody StartRtmpRequest request) {
//        String sessionId = digitalHumanService.startRtmpInstance(
//            request.getUserId(),
//            request.getUserName(),
//            request.getRtmpUrl(),
//            request.getRtmpToken()
//        );
//        return Collections.singletonMap("sessionId", sessionId);
//    }
    
    /**
     * 发送播报文本
     * @param request 文本请求
     */
    @PostMapping("/speak")
    public ResponseEntity<Void> speakText(@RequestBody SpeakRequest request) {
        digitalHumanService.sendTextMessage(
            request.getSessionId(), 
            request.getText()
        );
        return ResponseEntity.ok().build();
    }
    
    /**
     * 结束数字人会话
     * @param sessionId 会话ID
     */
    @PostMapping("/stop/{sessionId}")
    public ResponseEntity<Void> stopInstance(@PathVariable String sessionId) {
        digitalHumanService.stopInstance(sessionId);
        return ResponseEntity.ok().build();
    }
    
    /**
     * 查询会话状态
     * @param sessionId 会话ID
     */
    @GetMapping("/status/{sessionId}")
    public QueryRunningInstanceResponse getStatus(@PathVariable String sessionId) {
        return digitalHumanService.queryInstance(sessionId);
    }

    /**
     * 获取 sessionId
     * @param
     * @return
     */
    @PostMapping("/getSessionId")
    public void getSessionId(String sessionId) {
        //存储sessionId
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("sessionId",sessionId);
    }

}

// DTO定义
@Data
class StartRequest {
    private String userId;
    private String userName;
}

@Data
class StartRtmpRequest extends StartRequest {
    private String rtmpUrl;
    private String rtmpToken;
}

@Data
class SpeakRequest {
    private String sessionId;
    private String text;
}